const express = require("express");
const multer = require("multer");
const cors = require("cors");

const app = express();
app.use(cors());

// 存储配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => cb(null, "uploads/"),
  filename: (req, file, cb) => cb(null, Date.now() + "-" + file.originalname),
});

const upload = multer({ storage });

// 上传接口
app.post("/upload", upload.single("file"), (req, res) => {
  res.json({
    message: "文件上传成功",
    filename: req.file.filename,
  });
});

// 确保上传目录存在
const fs = require("fs");
const uploadDir = "uploads";
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir);
}

app.listen(5000, () => {
  console.log("后端服务运行在 http://localhost:5000");
});
